package org.jeecg.modules.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.course.entity.GwCourse;
import org.jeecg.modules.course.entity.GwCourseStudent;
import org.jeecg.modules.course.service.IGwCourseService;
import org.jeecg.modules.course.service.IGwCourseStudentService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: 学生课程
 * @Author: jeecg-boot
 * @Date:   2024-09-30
 * @Version: V1.0
 */
@Slf4j
@Api(tags="学生课程")
@RestController
@RequestMapping("/course/gwCourseStudent")
public class GwCourseStudentController extends JeecgController<GwCourseStudent, IGwCourseStudentService> {

	@Resource
	private IGwCourseStudentService gwCourseStudentService;

	@Resource
	private IGwCourseService iGwCourseService;
	
	/**
	 * 分页列表查询
	 *
	 * @param gwCourseStudent
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "学生课程-分页列表查询")
	@ApiOperation(value="学生课程-分页列表查询", notes="学生课程-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(GwCourseStudent gwCourseStudent,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GwCourseStudent> queryWrapper = QueryGenerator.initQueryWrapper(gwCourseStudent, req.getParameterMap());
		Page<GwCourseStudent> page = new Page<GwCourseStudent>(pageNo, pageSize);
		IPage<GwCourseStudent> pageList = gwCourseStudentService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param gwCourseStudent
	 * @return
	 */
	@AutoLog(value = "学生课程-选课接口")
	@ApiOperation(value="学生课程-选课接口", notes="学生课程-选课接口")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GwCourseStudent gwCourseStudent) {

		// 验证课程是否存在
		final GwCourse gwCourse = iGwCourseService.getById(gwCourseStudent.getCourseId());
		if(gwCourse == null) {
			return Result.error("选择的课程不存在！");
		}

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		QueryWrapper<GwCourseStudent> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("student_id", sysUser.getId());
		queryWrapper.eq("course_id", gwCourseStudent.getCourseId());
		if(gwCourseStudentService.count(queryWrapper) > 0) {
			return Result.error("已经选择该课程， 请勿重复选择！");
		}

		gwCourseStudent.setStudentId(sysUser.getId());
		gwCourseStudentService.save(gwCourseStudent);
		return Result.OK("选课成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param gwCourseStudent
	 * @return
	 */
	@AutoLog(value = "学生课程-编辑")
	@ApiOperation(value="学生课程-编辑", notes="学生课程-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> edit(@RequestBody GwCourseStudent gwCourseStudent) {
		gwCourseStudentService.updateById(gwCourseStudent);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "学生课程-通过id删除")
	@ApiOperation(value="学生课程-通过id删除", notes="学生课程-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		gwCourseStudentService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "学生课程-批量删除")
	@ApiOperation(value="学生课程-批量删除", notes="学生课程-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.gwCourseStudentService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "学生课程-通过id查询")
	@ApiOperation(value="学生课程-通过id查询", notes="学生课程-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GwCourseStudent gwCourseStudent = gwCourseStudentService.getById(id);
		return Result.OK(gwCourseStudent);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param gwCourseStudent
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, GwCourseStudent gwCourseStudent) {
      return super.exportXls(request, gwCourseStudent, GwCourseStudent.class, "学生课程");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, GwCourseStudent.class);
  }

}
